﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace DemoApp
{
    /// <summary>
    /// Interaction logic for PopupEditWindow.xaml
    /// </summary>
    public partial class PopupEditWindow : Window
    {
        List<string> methodNames, methodCodes;
        Dictionary<int, string> language;
        int currentIndex;

        public PopupEditWindow(ref Dictionary<int, string> language, ref List<string> methodNames, ref List<string> methodCodes)
        {
            InitializeComponent();

            this.language = language;
            this.methodNames = methodNames;
            this.methodCodes = methodCodes;
            RefreshListView();
        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
            if (language.Count == 0)
                this.language.Add(cmbLanguage.SelectedIndex+1, cmbLanguage.Text);
            this.methodNames.Add(txtMethodName.Text);
            this.methodCodes.Add(txtMethodCode.Text);
            RefreshListView();
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            this.methodNames[currentIndex] = txtMethodName.Text;
            this.methodCodes[currentIndex] = txtMethodCode.Text;
            RefreshListView();
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            var messageBoxService = ApplicationServicesProvider.Instance.Provider.MessageBoxService;
            if (messageBoxService.ShowYesNo("Are you sure you want to delete method " + methodNames[currentIndex] + "?", 
                CustomDialogIcons.Question) == CustomDialogResults.Yes)
            {
                this.methodNames.RemoveAt(currentIndex);
                this.methodCodes.RemoveAt(currentIndex);
                RefreshListView();
            }
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void lvMethodList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                currentIndex = lvMethodList.SelectedIndex;
                txtMethodName.Text = methodNames[currentIndex];
                txtMethodCode.Text = methodCodes[currentIndex];
                btnUpdate.IsEnabled = true;
                btnDelete.IsEnabled = true;
            }
            catch (Exception) { }
        }

        private void RefreshListView()
        {
            lvMethodList.ItemsSource = this.methodNames;
            ICollectionView view = CollectionViewSource.GetDefaultView(lvMethodList.ItemsSource);
            view.Refresh();
            if (lvMethodList.Items.Count == 0)
            {
                cmbLanguage.Items.Add("Java");
                cmbLanguage.Items.Add("C#");
            }
            else if (lvMethodList.Items.Count > 0)
            {
                foreach (var value in language.Values)
                    cmbLanguage.Items.Add(value);
                cmbLanguage.IsEnabled = false;
            }
            txtMethodName.Text = "";
            txtMethodCode.Text = "";
            btnUpdate.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }
    }
}
